/**
 * SelectableOption.java
 *
 * Copyright 2010 Jack Leow
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package my.edu.clhs.springframework.web.servlet.view.stringtemplate3.support;

/**
 * UI-oriented Java Bean that represents an option that has an on/off state to
 * indicate if it is current selected. Useful for backing select/combo boxes,
 * radio buttons, check boxes and any other UI widgets that have on/off states.
 * 
 * @author Jack Leow
 * @version 1.0
 * @since 1.0 (Dec 14, 2010, 3:03:00 PM)
 */
// TODO genericize when we move to Java 5.
class SelectableOption {
    public SelectableOption(Object value, boolean selected) {
        this.value = value;
        this.selected = selected;
    }
    
    private final Object value;
    
    public Object getValue() {
        return value;
    }
    
    private final boolean selected;
    
    public boolean isSelected() {
        return selected;
    }
    
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (selected ? 1231 : 1237);
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        return result;
    }
    
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SelectableOption other = (SelectableOption) obj;
        if (selected != other.selected)
            return false;
        if (value == null) {
            if (other.value != null)
                return false;
        } else if (!value.equals(other.value))
            return false;
        return true;
    }
    
    public String toString() {
        return (value != null ? value.toString() : "null") +
            " (" + (selected ? "On" : "Off") + ")";
    }
}